package com.wutianyi.module.product.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.wutianyi.module.product.ProductAttrDao;
import com.wutianyi.module.product.dataobject.ProductAttrDO;
import com.wutianyi.utils.Utils;

import static com.wutianyi.entity.DataStoreField.DS_P_PID;
import static com.wutianyi.entity.DataStoreField.DS_P_VALUE;

import static com.wutianyi.entity.EntityTypeEnum.ProductAttr;

public class ProductAttrDaoDataStoreImpl implements ProductAttrDao {
	
	private DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
	
	private void setEntity(ProductAttrDO productAttr) {
		int id = 0 != productAttr.getId() ? productAttr.getId() : productAttr.hashCode();
		productAttr.setGmtModified(new Date());
		Entity entity = new Entity(ProductAttr.name(), id);
		entity.setProperty(DS_P_VALUE, new Text(Utils.serialize(productAttr)));
		entity.setProperty(DS_P_PID, productAttr.getProductId());
		datastoreService.put(entity);
		productAttr.setId(id);
	}
	
	@Override
	public ProductAttrDO getProductAttr(int id) {
		Entity entity = datastoreService.prepare(
				new Query(KeyFactory.createKey(ProductAttr.name(), id))).asSingleEntity();
		
		if (null != entity) {
			ProductAttrDO productAttr = (ProductAttrDO) Utils.unSerialize(entity
					.getProperty(DS_P_VALUE));
			productAttr.setId((int) entity.getKey().getId());
			return productAttr;
		}
		
		return null;
	}
	
	@Override
	public List<ProductAttrDO> listProductAttrsByProductId(int productId) {
		Query query = new Query(ProductAttr.name());
		query.addFilter(DS_P_PID, FilterOperator.EQUAL, productId);
		
		Iterable<Entity> iters = datastoreService.prepare(query).asIterable();
		List<ProductAttrDO> productAttrs = new ArrayList<ProductAttrDO>();
		
		for (Entity entity : iters) {
			ProductAttrDO productAttr = (ProductAttrDO) Utils.unSerialize(entity
					.getProperty(DS_P_VALUE));
			productAttr.setId((int) entity.getKey().getId());
			productAttrs.add(productAttr);
		}
		
		return productAttrs;
	}
	
	@Override
	public int createProductAttr(ProductAttrDO productAttr) {
		productAttr.setId(0);
		setEntity(productAttr);
		return 1;
	}
	
	@Override
	public void createProductAttrs(List<ProductAttrDO> productAttrs) {
		if (CollectionUtils.isEmpty(productAttrs)) {
			return;
		}
		for (ProductAttrDO productAttr : productAttrs) {
			createProductAttr(productAttr);
		}
	}
	
	@Override
	public int deleteProductAttrsByPId(int pId) {
		Query query = new Query(ProductAttr.name());
		query.setKeysOnly();
		query.addFilter(DS_P_PID, FilterOperator.EQUAL, pId);
		List<Key> keys = new ArrayList<Key>();
		Iterable<Entity> iters = datastoreService.prepare(query).asIterable();
		for (Entity entity : iters) {
			keys.add(entity.getKey());
		}
		datastoreService.delete(keys);
		return 1;
	}
	
}
